
package com.csc440.team2.intentRecievers;

import java.util.Date;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


import com.csc440.team2.SmilConstants;
import com.csc440.team2.activity.R;
import com.csc440.team2.activity.SMILPlayer;
import com.csc440.team2.database.entities.Message;
import com.csc440.team2.smil.SMILMessage;
import com.csc440.team2.smil.SMILReadException;
import com.csc440.team2.smil.SMILReader;


public class SmilReciever extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        try
        {
            Bundle extras = intent.getExtras();
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Message message;
            if (extras != null)
            {
                message = (Message) extras.getSerializable(SmilConstants.MESSAGE);
                SMILMessage smilMessage;

                smilMessage = SMILReader.parseMessage(message.getMessage());

                Intent playerIntent = new Intent(context, SMILPlayer.class);
                playerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                playerIntent.putExtra(SmilConstants.MESSAGE, smilMessage);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, playerIntent,
                        PendingIntent.FLAG_ONE_SHOT);
                Notification notification = new Notification(R.drawable.icon, "New SMIL message", (new Date())
                        .getTime());
                notification.setLatestEventInfo(context, "New SMIL Message", "Message from Dave", pendingIntent);
                notificationManager.notify((int) message.getId(), notification);
            }
        }
        catch (SMILReadException e)
        {
            Log.e("SmilReceiver", "Could not parse received SMIL message.", e);
        }
    }
}
